package cn.zhiwen.lambdaDemo;

import java.util.Arrays;
import java.util.Comparator;

public class SortLambdaDemo {
    public static void main(String[] args) {
        String[] players = {"Rafael Nadal", "Novak Djokovic",
                "Stanislas Wawrinka", "David Ferrer",
                "Roger Federer", "Andy Murray",
                "Tomas Berdych", "Juan Martin Del Potro",
                "Richard Gasquet", "John Isner"};
        //传统方式
        Arrays.sort(players, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        for (String player : players) {
            System.out.println(player+"===");
        }

        String[] players2 = {"Rafael Nadal", "Novak Djokovic",
                "Stanislas Wawrinka", "David Ferrer",
                "Roger Federer", "Andy Murray",
                "Tomas Berdych", "Juan Martin Del Potro",
                "Richard Gasquet", "John Isner"};
        //lambda方式
        Comparator<String> comparator = (String o1,String o2)->(o1.compareTo(o2));
        Arrays.sort(players2,comparator);
        for (String player : players2) {
            System.out.print(player+"===");
        }

        String[] players3 = {"Rafael Nadal", "Novak Djokovic",
                "Stanislas Wawrinka", "David Ferrer",
                "Roger Federer", "Andy Murray",
                "Tomas Berdych", "Juan Martin Del Potro",
                "Richard Gasquet", "John Isner"};
        //更为简洁的方式
        Arrays.sort(players3,(String o1,String o2)->(o1.compareTo(o2)));
        for (String player : players3) {
            System.out.println(player+"===");
        }
    }
}
